; (function($, window, undefined) {

    var win = $(window),
        doc = $(document),
        count = 1,
        isLock = false;

    var Dialog = function(options) {

        this.settings = $.extend({}, Dialog.defaults, options);
        this.init();

    }

    Dialog.prototype = {

        /**
        * 初始化
        */
        init: function() {
            var me = this;
            this.create();

            if (this.settings.lock) {
                this.lock();
            }

            if (!isNaN(this.settings.time) && this.settings.time != null) {
                this.time();
            }

            $(window).resize(function() {
                me.size();
                me.position();
            });
        },

        /**
        * 创建
        */
        create: function() {
            var skin = this.settings.skin;
            var skinCss = "";
            if (skin) {
                skinCss = " rDialog-" + skin
            }
            var divHeader = (this.settings.title == null) ? '' : '<div class="rDialog-header rDialog-header-' + this.settings.title + '">' + (this.settings.titleText || "") + '</div>';

            // HTML模板
            var templates = '<div class="rDialog-wrap">' +
                                divHeader +
                                '<div class="rDialog-content">' + this.settings.content + '</div>' +
                                '<div class="rDialog-footer"></div>' +
                            '</div>';

            // 追回到body
            this.dialog = $('<div>').addClass('rDialog' + skinCss).css({ zIndex: this.settings.zIndex + (count++) }).html(templates).prependTo('body');

            // 设置ok按钮
            if ($.isFunction(this.settings.ok)) {
                this.ok(this.settings.sure);
            }
            // 设置cancel按钮
            if ($.isFunction(this.settings.cancel)) {
                this.cancel();
            }


            if (this.settings.hideHead) {
                this.dialog.find(".rDialog-header").hide();
            }

            // 设置大小
            this.size();

            // 设置位置
            this.position();

        },

        /**
        * ok
        */
        ok: function(sure) {
            var _this = this,
                footer = this.dialog.find('.rDialog-footer');
            if(sure){
                footer.append()
                $("<a href='javascript:;'>"+sure+"</a>")
                    .on("click",function(){
                        _this.close();
                        var okCallback = _this.settings.ok();
                    })
                    .addClass('rDialog-ok').prependTo(footer);
            }else{
                footer.append()
                $("<a href='javascript:;'>"+this.settings.okText+"</a>")
                    .on("click",function(){
                        _this.close();
                        var okCallback = _this.settings.ok();
                    })
                    .addClass('rDialog-ok').prependTo(footer);
            }
        },

        /**
        * cancel
        */
        cancel: function() {
            
            var _this = this,
                footer = this.dialog.find('.rDialog-footer');
            footer.append()
            $("<a href='javascript:;'>"+this.settings.cancelText+"</a>")
            .on("click",function(){
                var cancelCallback = _this.settings.cancel();
                if (cancelCallback == undefined || cancelCallback) {
                    _this.close();
                }
            })
            .addClass('rDialog-cancel').prependTo(footer);

        },

        /**
        * 设置大小
        */
        size: function() {

            var content = this.dialog.find('.rDialog-content'),
            	wrap = this.dialog.find('.rDialog-wrap');

            content.css({
                width: this.settings.width,
                height: this.settings.height
            });
            //wrap.width(content.width());
        },

        /**
        * 设置位置
        */
        position: function() {

            var _this = this,
                winWidth = win.width(),
                winHeight = win.height(),
                scrollTop = 0;

            this.dialog.css({
                left: (winWidth - _this.dialog.width()) / 2,
                top: (winHeight - _this.dialog.height()) / 2 + scrollTop
            });

        },

        /**
        * 设置锁屏
        */
        lock: function() {
            if($(".rDialog-mask").size() < 1) {
                 this.lockLayer = $('<div>').css({ zIndex: this.settings.zIndex }).addClass('rDialog-mask');
                 this.lockLayer.appendTo('body');
            } else{
                $(".rDialog-mask").show();
            }
        },

        /**
        * 关闭锁屏
        */
        unLock: function() {
            if (this.settings.lock) {
                $(".rDialog-mask").hide();
            }
        },

        /**
        * 关闭方法
        */
        close: function() {
            this.dialog.remove();
            this.unLock();
        },

        /**
        * 定时关闭
        */
        time: function() {

            var _this = this;

            this.closeTimer = setTimeout(function() {
                _this.close();
            }, this.settings.time);

        }

    }

    /**
    * 默认配置
    */
    Dialog.defaults = {

        // 内容
        content: '加载中...',

        // 标题
        title: 'load',

        // 宽度
        width: 'auto',

        // 高度
        height: 'auto',

        // 确定按钮回调函数
        ok: null,

        // 取消按钮回调函数
        cancel: null,

        // 确定按钮文字
        okText: '确定',

        // 取消按钮文字
        cancelText: '取消',

        // 自动关闭时间(毫秒)
        time: null,

        // 是否锁屏
        lock: true,
        // z-index值
        zIndex: 9999

    }

    var rDialog = function(options) {
        return new Dialog(options);
    }

    window.rDialog = $.rDialog = $.dialog = rDialog;

})(window.jQuery || window.Zepto, window);